﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

namespace Zxb.DayWork.Logic
{
    public class CaseHitWorker : BatchWorker
    {
        private DataTable caseLastDayDT;
        private DataTable caseWeekDT;
        private DataTable caseMonthDT;

        private DataTable proLastDayDT;
        private DataTable proWeekDT;
        private DataTable proMonthDT;

        private DateTime currentDay;
        private DateTime lastDay;
        private DateTime WeekDay;
        private DateTime MonthDay;

        private static DataTable ReadDtHit(DateTime start, DateTime end, int objid)
        {
            string sql = string.Format("select ObjectID as CID,SUM(RandCount) as Hits from HitsWeiKe_Real with(nolock) where VisitTime>='{0}' and VisitTime<'{1}' and TypeID={2} group by ObjectID ", start.ToString("yyyy-MM-dd 00:00"), end.ToString("yyyy-MM-dd 00:00"), objid);
            return DAL.ReadDt_Retry(DB.Home_logAdmin, sql);
        }

        public CaseHitWorker()
        {
            currentDay = DateTime.Now;
            lastDay = currentDay.AddDays(-1.0);
            WeekDay = currentDay.AddDays(-7.0);
            MonthDay = currentDay.AddDays(-30.0);
        }

        protected override void ReadData()
        {
            caseLastDayDT = ReadDtHit(lastDay, currentDay, 5);
            caseWeekDT = ReadDtHit(WeekDay, currentDay, 5);
            caseMonthDT = ReadDtHit(MonthDay, currentDay, 5);

            proLastDayDT = ReadDtHit(lastDay, currentDay, 6);
            proWeekDT = ReadDtHit(WeekDay, currentDay, 6);
            proMonthDT = ReadDtHit(MonthDay, currentDay, 6);
        }

        protected override void ProcessData()
        {

        }

        protected override void WriteData()
        {
            DAL.TVPUpdate_Retry(caseLastDayDT, "Cases_Update_LastDayHits", "UdtCases_Update_LastDayHits", "TaskWriter");
            DAL.TVPUpdate_Retry(caseWeekDT, "Cases_Update_LastWeekHits", "UdtCases_Update_LastWeekHits", "TaskWriter");
            DAL.TVPUpdate_Retry(caseMonthDT, "Cases_Update_LastMonthHits", "UdtCases_Update_LastMonthHits", "TaskWriter");

            DAL.TVPUpdate_Retry(proLastDayDT, "CompanyNews_Update_LastDayHits", "UdtCompanyNews_Update_LastDayHits", "TaskWriter");
            DAL.TVPUpdate_Retry(proWeekDT, "CompanyNews_Update_LastWeekHits", "UdtCompanyNews_Update_LastWeekHits", "TaskWriter");
            DAL.TVPUpdate_Retry(proMonthDT, "CompanyNews_Update_LastMonthHits", "UdtCompanyNews_Update_LastMonthHits", "TaskWriter");
        }
    }
}
